/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.gui.aceros;

import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.table.DefaultTableModel;

import ar.com.wholeconsulting.pacc.model.Acero;

/**
 *
 * @author Duco Pablo
 */
public class AceroModel extends DefaultTableModel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 593048011321737192L;

	public AceroModel(Object[][] data, Object[] columnNames) {
        super(data, columnNames);
    }

    public void addRow(Acero acero) {
        Object[] vector = new Object[14];
        try {
            for (AceroColum steel : AceroColum.values()) {
                if(steel.getListIndex()!=-1){
                    vector[steel.getTableIndex()] = acero.getyValues().get(steel.getListIndex());
                } else {
                    vector[steel.getTableIndex()] = acero.getNombre();
                }
            }
            vector[0] = acero;
            super.addRow(vector);
        } catch (Exception ex) {
            Logger.getLogger(AceroModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("all")
    @Override
    public void setValueAt(Object aValue, int row, int column) {
        try {
            AceroColum steel = AceroColum.fromTableIndex(column);
            Method method = Acero.class.getMethod(AceroColum.getterMethodName());
            Acero obj = (Acero) super.getValueAt(row, 0);
            if (steel.getListIndex() != -1) {
                
				List list = (List) method.invoke(obj);
                list.set(steel.getListIndex(), aValue);
            } else {
                obj.setNombre((String) aValue);
            }
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(AceroModel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AceroModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public Object getValueAt(int row, int column) {
        try {
            AceroColum steel = AceroColum.fromTableIndex(column);
            //fluid.getterMethodName()
            //Method method = Acero.class.getMethod(AceroColum.getterMethodName());
            Acero obj = (Acero) super.getValueAt(row, 0);
            if (steel.getListIndex() != -1) {
                return obj.getyValues().get(steel.getListIndex());
            } else {
                return obj;
            }
        } catch (Exception ex) {
            Logger.getLogger(AceroModel.class.getName()).log(Level.SEVERE, null, ex);
        } 
        throw new IllegalArgumentException();
    }
}
